
package ejb;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class test {

	public static void main(String[] args) {
		
		Session session = null;
		MessageProducer producer = null;

		String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
		
		String DEFAULT_DESTINATION = "jms/queue/articulo";
		String DEFAULT_USERNAME = "test1";
		String DEFAULT_PASSWORD = "test1234";

		
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Destination destination = null;
		Context context = null;

		try {
			context =getInitialContext(context);
			// Perform the JNDI lookups
			String connectionFactoryString = DEFAULT_CONNECTION_FACTORY;
			connectionFactory = (ConnectionFactory) context
					.lookup(connectionFactoryString);

			String destinationString = System.getProperty("destination",
				DEFAULT_DESTINATION);
			destination = (Destination) context.lookup(destinationString);

			// Create the JMS connection, session, producer, and consumer
			connection = connectionFactory.createConnection(
					System.getProperty("username", DEFAULT_USERNAME),
					System.getProperty("password", DEFAULT_PASSWORD));
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			producer = session.createProducer(destination);
			connection.start();
			TextMessage textMessage = null;
			textMessage = session.createTextMessage();
			textMessage.setText("Hola maxi");
			producer.send(textMessage);
			connection.stop();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				producer.close();
				session.close();
				connection.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		} 
	}
	public static Context getInitialContext(Context initialContext) throws NamingException {
		if (initialContext == null) {

			Properties jndiProps = new Properties();
			jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
			jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jboss.naming.remote.client.InitialContextFactory");
			jndiProps.put(Context.PROVIDER_URL, "remote://localhost:4447");
			// username
			jndiProps.put(Context.SECURITY_PRINCIPAL, "test1");
			// password
			jndiProps.put(Context.SECURITY_CREDENTIALS, "test1234");
			// This is an important property to set if you want to do EJB
			// invocations via the remote-naming project
			jndiProps.put("jboss.naming.client.ejb.context", true);
			// create a context passing these properties
			initialContext = new InitialContext(jndiProps);
		}
		return initialContext;
	}

}